const Joi = require("joi");
module.exports = class ProductController extends colpu.Controller {
  /**
   * @api {get} /product/list 用户上传图片
   * @apiDescription 用户上传图片
   * @apiGroup 产品列表
   * @apiVersion 1.0.0
   *
   * @apiSuccess {Number} status 状态码
   * @apiSuccess {String} message 消息
   * @apiSuccess {Object} data 返回结果
   * @apiSuccessExample {json} Success-Response:
   * HTTP/1.1 200 OK
   *  {
   *    "data": true,
   *    "status": 0,
   *    "message": "ok"
   *  }
   */
  async getUserBackgroundList(ctx) {
    await ctx.validateAsync({
      query: {
        uid: Joi.string().required(),
        page: Joi.number().integer(),
        size: Joi.number().integer(),
      },
      status: 10001,
    });
    const { uid, page = 1, size = 10 } = ctx.query;
    const res = await this.service.product.getUserBackgroundList({
      uid,
      page,
      size,
    });
    ctx.respondEncrypt(res);
  }

  /**
   * @api {get} /product/background 获得单个用户上传图片
   * @apiDescription 获得单个用户上传图片
   * @apiGroup 产品列表
   * @apiVersion 1.0.0
   *
   * @apiSuccess {Number} status 状态码
   * @apiSuccess {String} message 消息
   * @apiSuccess {Object} data 返回结果
   * @apiSuccessExample {json} Success-Response:
   * HTTP/1.1 200 OK
   *  {
   *    "data": true,
   *    "status": 0,
   *    "message": "ok"
   *  }
   */
  async getUserBackground(ctx) {
    await ctx.validateAsync({
      query: {
        uid: Joi.string().required(),
        id: Joi.number().required(),
      },
      status: 10001,
    });
  }

  /**
   * @api {post} /product/background 用户上传图片
   * @apiDescription 用户上传图片
   * @apiGroup 产品列表
   * @apiVersion 1.0.0
   *
   * @apiSuccess {Number} status 状态码
   * @apiSuccess {String} message 消息
   * @apiSuccess {Object} data 返回结果
   * @apiSuccessExample {json} Success-Response:
   * HTTP/1.1 200 OK
   *  {
   *    "data": true,
   *    "status": 0,
   *    "message": "ok"
   *  }
   */
  async postUserBackground(ctx) {
    await ctx.validateAsync({
      body: {
        uid: Joi.string().required(),
        pro_id: Joi.number().required(),
        name: Joi.string().required(),
        describe: Joi.string(),
        src: Joi.string().required(),
        thumb: Joi.string().required(),
        deep: Joi.number(),
        sort: Joi.number(),
      },
      status: 10001,
    });
    const res = await this.service.product.uploadUserBackground(
      ctx.request.body
    );
    console.log(res);
    ctx.respond(res, 0, "更新成功");
  }

  /**
   * @api {put} /product/background 用户上传图片修改
   * @apiDescription 用户上传图片修改
   * @apiGroup 产品列表
   * @apiVersion 1.0.0
   *
   * @apiSuccess {Number} status 状态码
   * @apiSuccess {String} message 消息
   * @apiSuccess {Object} data 返回结果
   * @apiSuccessExample {json} Success-Response:
   * HTTP/1.1 200 OK
   *  {
   *    "data": true,
   *    "status": 0,
   *    "message": "ok"
   *  }
   */
  async putUserBackground(ctx) {
    await ctx.validateAsync({
      body: {
        id: Joi.number().required(),
        uid: Joi.string().required(),
      },
      status: 10001,
    });
    const res = await this.service.product.putUserBackground(ctx.request.body);
    if (res === 1) {
      ctx.respond(null, 0, "更新成功");
    } else {
      ctx.respond(null, -1, "没有任何更新");
    }
  }
};
